package com.wd.jetpack.demo.main.view

import android.content.Context
import android.os.SystemClock
import android.util.AttributeSet
import android.widget.Chronometer
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent

/**
 * 自定义控件 生命周期监听<BR>
 *
 * @author W4255
 * @version [V0.0.1, 2021/6/26]
 * @Description: NA
 */
class MyChronometer constructor(context: Context, attrs: AttributeSet? = null)
    : Chronometer(context, attrs), LifecycleObserver {
    private var elapsedTime = 0L

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun startMeter() {
        this.base = SystemClock.elapsedRealtime() - elapsedTime
        start()
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun stopMeter() {
        elapsedTime = SystemClock.elapsedRealtime() - base
        stop()
    }
}